//
//  MapViewController.m
//  QuickCure1
//
//  Created by Ashzenki on 05/11/2016.
//  Copyright © 2016 maoge. All rights reserved.
//

#import "MapViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "CustomAnnotation.h"


@interface MapViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>

@end

@implementation MapViewController {
    
    CLLocationManager *_locManager;
    MKMapView *_mapView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建地图view
    _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_mapView];
    
    //显示用户位置,地图类型,是否跟踪用户位置
    _mapView.showsUserLocation = YES;
    _mapView.mapType = MKMapTypeStandard;
    _mapView.userTrackingMode=MKUserTrackingModeFollow;
    
    //创建定位
    _locManager = [[CLLocationManager alloc] init];
    
    //定位代理和精度
    _locManager.delegate = self;
    _locManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    
    //开启定位
    [_locManager startUpdatingLocation];
}

#pragma mark - 返回新位置,旧位置
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
    NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
    NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
    
    NSLog(@"Lat: %@  Lng: %@", strLat, strLng);
    
    //大头针
    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);
    float zoomLevel = 0.02;
    
    //设置地图显示区域
    MKCoordinateRegion region = MKCoordinateRegionMake(coords,MKCoordinateSpanMake(zoomLevel, zoomLevel));
    [_mapView setRegion:[_mapView regionThatFits:region] animated:YES];
}

#pragma mark - 定位失败调用
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    
    NSLog(@"locError:%@", error);
}

#pragma mark - 实时更新定位信息
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {


    [locations enumerateObjectsUsingBlock:^(CLLocation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"定位信息:%@", obj);
    }];
}


@end


































